﻿using LojaVirtual.Geral.Constantes;
using PersistLayer.DAL;
using PersistLayer.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace LojaVirtual.Site
{
	public abstract class BaseSite : System.Web.UI.Page
	{
		public Dictionary<string, string> dicRedirect = new Dictionary<string, string>();
		public Cliente Cli
		{
			get
			{
				return new ClienteDAL().Obter(1);
			}
		}
		public Loja Lj
		{
			get
			{
				return new LojaDAL().Obter(1);
			}
		}
		public Currency Cur
		{
			get
			{
				if (Cli == null)
					return new CurrencyDAL().ObterPorSigla("USD");

				return Cli.Currency;
			}
		}

		public void PageLoad()
		{
			CarregaTexto();
			CarregaDados();
		}

		public abstract void CarregaTexto();
		public abstract void CarregaDados();

		public string Redirecionar(string url, Dictionary<string, string> parametros)
		{
			string redir = "";

			redir = "/" + url + "?";

			bool hasParameters = false;

			foreach (KeyValuePair<string, string> item in parametros)
			{
				hasParameters = true;
				redir += item.Value + "=" + Util.Criptografia.Encriptar(CRIPTO.CHAVE, CRIPTO.VETOR, item.Key) + "&";
			}

			if (hasParameters)
				return redir.Substring(0, redir.Length - 1);
			else
				return redir;
		}

		public decimal CalcularDesconto(decimal preco, int desconto)
		{
			return preco - ((preco * desconto) / 100);
		}

		public decimal CurrencyConverter(string siglaTo)
		{
			try
			{
				HttpWebRequest requisicao = (HttpWebRequest)WebRequest.Create("http://rate-exchange.appspot.com/currency?from=USD&to=" + siglaTo);
				HttpWebResponse resposta = (HttpWebResponse)requisicao.GetResponse();

				int cont;
				byte[] buffer = new byte[1000];
				StringBuilder sb = new StringBuilder();
				string temp;
				Stream stream = resposta.GetResponseStream();

				do
				{
					cont = stream.Read(buffer, 0, buffer.Length);
					temp = Encoding.Default.GetString(buffer, 0, cont).Trim();
					sb.Append(temp);
				} while (cont > 0);

				string pagina = sb.ToString();

				int fid = pagina.IndexOf("rate\":") + 7;
				string res = pagina.Substring(fid);
				res = res.Substring(0, res.IndexOf(","));

				return Convert.ToDecimal(res);
			}
			catch (Exception)
			{
				throw;
			}
		}

		#region Cookies
		public void AdicionarCookie(string Name, string Value, int hoursToExpire)
		{
			HttpCookie cookie = new HttpCookie(Name);
			cookie.Value = Value;
			cookie.Expires = DateTime.Now.AddHours(hoursToExpire);
			Response.Cookies.Add(cookie);
		}
		public string RecuperarCookie(string Name)
		{
			HttpCookie cookie = Request.Cookies[Name];
			if (cookie != null)
				return cookie.Value;
			else
				return string.Empty;
		}
		public void DeletarCookie(string Name)
		{
			HttpCookie cookie = Request.Cookies[Name];
			if (cookie != null)
			{
				cookie.Expires = DateTime.Now.AddHours(-2);
				Response.Cookies.Add(cookie);
			}
		}
		#endregion
	}
}